﻿namespace Digitalmes.Application.Sys.Users.Queries;

/// <summary>
/// 通过用户账号查询用户信息，其中会返回结果会包含用户拥有的角色集合。
/// </summary>
/// <param name="UserName"></param>
public record GetUserByNameQuery(string UserName) : IRequest<IResult<SysUserDto?>>;

internal sealed class GetRoleByNamedQueryHandler(IRepository<SysUser> repo) : IRequestHandler<GetUserByNameQuery, IResult<SysUserDto?>>
{
    public async Task<IResult<SysUserDto?>> Handle(GetUserByNameQuery request, CancellationToken cancellationToken)
    {
        var role = await repo.AsQueryable()
            .Includes(m => m.UserRoles, it => it.SysRole)
            .FirstOrDefaultAsync(s => s.UserName == request.UserName, cancellationToken);
        return Result.From(role.Adapt<SysUserDto?>());
    }
}
